from pydantic import BaseModel, Field, validator
from typing import Optional, List
from uuid import UUID
from datetime import datetime
from app.models.maintenance_record import MaintenanceType, MaintenanceStatus

# 维护记录基础模型
class MaintenanceRecordBase(BaseModel):
    maintenance_type: MaintenanceType
    title: str = Field(..., min_length=1, max_length=255)
    description: Optional[str] = None
    priority: str = Field(default="medium", pattern="^(low|medium|high|urgent)$")
    scheduled_at: datetime
    estimated_duration: Optional[int] = Field(None, gt=0, description="预计持续时间(分钟)")
    assigned_to: Optional[UUID] = None

class MaintenanceRecordCreate(MaintenanceRecordBase):
    device_id: UUID

class MaintenanceRecordUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=255)
    description: Optional[str] = None
    priority: Optional[str] = Field(None, pattern="^(low|medium|high|urgent)$")
    status: Optional[MaintenanceStatus] = None
    scheduled_at: Optional[datetime] = None
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None
    estimated_duration: Optional[int] = Field(None, gt=0)
    actual_duration: Optional[int] = Field(None, gt=0)
    assigned_to: Optional[UUID] = None
    performed_by: Optional[UUID] = None
    result: Optional[str] = None
    issues_found: Optional[str] = None
    actions_taken: Optional[str] = None
    parts_replaced: Optional[str] = None
    cost: Optional[int] = Field(None, ge=0, description="成本(分为单位)")
    quality_rating: Optional[int] = Field(None, ge=1, le=5)
    is_successful: Optional[bool] = None
    requires_followup: Optional[bool] = None
    followup_date: Optional[datetime] = None

class MaintenanceRecordResponse(MaintenanceRecordBase):
    id: UUID
    device_id: UUID
    status: MaintenanceStatus
    started_at: Optional[datetime] = None
    completed_at: Optional[datetime] = None
    actual_duration: Optional[int] = None
    performed_by: Optional[UUID] = None
    result: Optional[str] = None
    issues_found: Optional[str] = None
    actions_taken: Optional[str] = None
    parts_replaced: Optional[str] = None
    cost: Optional[int] = None
    quality_rating: Optional[int] = None
    is_successful: Optional[bool] = None
    requires_followup: Optional[bool] = None
    followup_date: Optional[datetime] = None
    approved_by: Optional[UUID] = None
    approved_at: Optional[datetime] = None
    created_at: datetime
    updated_at: Optional[datetime] = None
    created_by: UUID
    
    class Config:
        from_attributes = True

# 维护记录完成更新模型
class MaintenanceRecordComplete(BaseModel):
    completed_at: datetime = Field(default_factory=datetime.now)
    actual_duration: Optional[int] = Field(None, gt=0)
    result: str = Field(..., min_length=1)
    issues_found: Optional[str] = None
    actions_taken: Optional[str] = None
    parts_replaced: Optional[str] = None
    cost: Optional[int] = Field(None, ge=0)
    quality_rating: int = Field(..., ge=1, le=5)
    is_successful: bool = True
    requires_followup: bool = False
    followup_date: Optional[datetime] = None

# 维护记录审核模型
class MaintenanceRecordApprove(BaseModel):
    approved: bool
    comments: Optional[str] = None

# 维护记录统计模型
class MaintenanceStatsResponse(BaseModel):
    total_records: int
    completed_records: int
    pending_records: int
    overdue_records: int
    completion_rate: float
    average_duration: Optional[float] = None
    total_cost: int
    
    class Config:
        from_attributes = True

# 维护记录查询参数
class MaintenanceRecordFilter(BaseModel):
    device_id: Optional[UUID] = None
    maintenance_type: Optional[MaintenanceType] = None
    status: Optional[MaintenanceStatus] = None
    priority: Optional[str] = None
    assigned_to: Optional[UUID] = None
    performed_by: Optional[UUID] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    is_overdue: Optional[bool] = None